/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmQuanLyMonHoc.java
 *
 * Created on Feb 19, 2013, 11:01:18 PM
 */
package design;

import Dao.MonHocDao;
import Entity.MonHocEntity;
import Util.ConSQL;
import Util.Validate;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFormattedTextField;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.MaskFormatter;
import javax.swing.text.html.HTMLDocument.HTMLReader.PreAction;

/**
 *
 * @author Thuyngaytho
 */
public class frmQuanLyMonHoc extends javax.swing.JFrame {

    String maPB, tenPB, matkhau;
    String maMon, tenMon;
    float SoTC, hesoCC, hesoGK, hesoKT;
    int CheckTC, CheckCC, CheckGK, CheckKT;
    Vector tableRecord = new Vector();
    Vector tableTitle = new Vector();
    ConSQL conn = new ConSQL();
    Validate val = new Validate();
    DefaultTableModel tableModel, modelTimKiem;
    ArrayList<MonHocEntity> arrMonHoc;
    MonHocEntity mhEntity;
    MonHocDao mhDAO = new MonHocDao();

    //private SubjectEntity subjectEntity;
    /** Creates new form frmQuanLyMonHoc */
    public frmQuanLyMonHoc() {
        initComponents();
        createTitle();
        cbbSearch();
        btnSua.setEnabled(false);
        btnXoa.setEnabled(false);
        LoadInformation();
    }

    public frmQuanLyMonHoc(String maPB, String tenPB, String matkhau) {
        initComponents();
        //khoi tao cac doi tuong trong form
        createTitle();
        cbbSearch();
        btnSua.setEnabled(false);
        btnXoa.setEnabled(false);
        this.maPB = maPB;
        this.tenPB = tenPB;
        this.matkhau = matkhau;
        lbMaPB.setText(maPB);
        lbTenPB.setText(tenPB);
        LoadInformation();
    }
//khoi tao cbb Search

    public void cbbSearch() {
        cbbSearch.removeAllItems();
        cbbSearch.addItem("Mã môn");
        cbbSearch.addItem("Tên môn");
    }
    //xoa cac TF

    public void clearTF() {
        tfMaMH.setText("");
        tfTenMH.setText("");
        fmHSCC.setText("");
        fmHSGK.setText("");
        fmSoTC.setText("");
        fmHSKT.setText("");
    }

    public void createTitle() {
        tableTitle.addElement("Mã môn học");
        tableTitle.addElement("Tên môn học");
        tableTitle.addElement("Số tín chỉ");
        tableTitle.addElement("Hệ số chuyên cần");
        tableTitle.addElement("Hệ số giữa kì");
        tableTitle.addElement("Hệ số kết thúc");
    }

    public void creatRecord(ResultSet rs) throws SQLException {
        while (rs.next()) {
            Vector data = new Vector();
            data.add(rs.getString(1).toString());
            data.add(rs.getString(2).toString());
            data.add(rs.getString(3).toString());
            data.add(rs.getString(4).toString());
            data.add(rs.getString(5).toString());
            data.add(rs.getString(6).toString());

            tableRecord.add(data);
        }
    }
    //load du lieu tu cac TF

    public void LoadInput() {
        CheckAs0();

        maMon = tfMaMH.getText();
        tenMon = tfTenMH.getText();
        try {
            SoTC = Integer.parseInt(fmSoTC.getText());
        } catch (NumberFormatException e) {
            CheckTC = 1;
        }
        try {
            hesoCC = Integer.parseInt(fmHSCC.getText());
        } catch (NumberFormatException e) {
            CheckCC = 1;
        }

        try {
            hesoGK = Integer.parseInt(fmHSGK.getText());
        } catch (NumberFormatException e) {
            CheckGK = 1;
        }
        try {
            hesoKT = Integer.parseInt(fmHSKT.getText());
        } catch (NumberFormatException e) {
            CheckKT = 1;
        }
    }

    public void CheckAs0() {

        CheckCC = 0;
        CheckGK = 0;
        CheckKT = 0;
        CheckTC = 0;

    }
    //load du lieu tu bang len tf

    public void LoadInformation() {
        tableModel = new DefaultTableModel();
        tableModel.setRowCount(0);

        Vector col = new Vector();
        col.addElement("Mã Môn Học");
        col.addElement("Tên Môn Học");
        col.addElement("Số Tín Chỉ");
        col.addElement("Hệ Số Chuyên Cần");
        col.addElement("Hệ Số Giữa Kỳ");
        col.addElement("Hệ Số Kết Thúc");

        tableModel.setColumnIdentifiers(col);
        arrMonHoc = mhDAO.getAll();
        for (int i = 0; i < arrMonHoc.size(); i++) {
            mhEntity = arrMonHoc.get(i);
            Vector row = new Vector();
            row.addElement(mhEntity.getMaMH());
            row.addElement(mhEntity.getTenMH());
            row.addElement(mhEntity.getSoTC());
            row.addElement(mhEntity.getHesoCC());
            row.addElement(mhEntity.getHesoGK());
            row.addElement(mhEntity.getHesoKT());
            tableModel.addRow(row);
        }
        tbContent.setModel(tableModel);
    }
    public void LoadSearch(String str, int id) {
        modelTimKiem = new DefaultTableModel();
        modelTimKiem.setRowCount(0);
        Vector col = new Vector();
        col.addElement("Mã Môn Học");
        col.addElement("Tên Môn Học");
        col.addElement("Số Tín Chỉ");
        col.addElement("Hệ Số Chuyên Cần");
        col.addElement("Hệ Số Giữa Kỳ");
        col.addElement("Hệ Số Kết Thúc");
        
        modelTimKiem.setColumnIdentifiers(col);
        
        if(id == 0){
//            arrMonHoc = mhDAO.getAll();
            arrMonHoc = mhDAO.timKiem(str, 0);
        }else{
            arrMonHoc = mhDAO.timKiem(str, 1);
        }
        
        for (int i = 0; i < arrMonHoc.size(); i++) {
            mhEntity = arrMonHoc.get(i);
            Vector row = new Vector();
            row.addElement(mhEntity.getMaMH());
            row.addElement(mhEntity.getTenMH());
            row.addElement(mhEntity.getSoTC());
            row.addElement(mhEntity.getHesoCC());
            row.addElement(mhEntity.getHesoGK());
            row.addElement(mhEntity.getHesoKT());
            modelTimKiem.addRow(row);
        }
        tbContent.setModel(modelTimKiem);
    }
    //bat loi cac tf

    public String CheckInput() throws Exception {
        LoadInput();
        String cMamon = "";
        String cTenmon = "";
        String cSoTC = "";
        String cHSCC = "";
        String cHSGK = "";
        String cHSKT = "";
        //kiem tra ma mon
        if (maMon.equals("")) {
            cMamon = "Mã môn trống!\n";
        } else {
            Statement st = conn.getCon().createStatement();
            ResultSet rs = st.executeQuery("select * from monHoc where maMH='" + maMon + "';");
            if (rs.next() == true) {
                cMamon = "Trùng mã môn học!\n";
            }
        }
        // kiem tra ten mon
        if (tenMon.equals("")) {
            cTenmon = "Tên môn trống!\n";
        } else {
            Statement st = (Statement) conn.getCon().createStatement();
            ResultSet rs = st.executeQuery("select * from monHoc where tenMH=N'" + maMon + "';");
            if (rs.next() == true) {
                cTenmon = "Trùng tên môn học!\n";
            }
        }
        //kiem tra so tin chi
        if (CheckTC == 1) {
            cSoTC = "Số tín chỉ phải là số!\n";
        } else {
            if (SoTC <= 0) {
                cSoTC = "Số tín chỉ phải lớn hơn 0!\n";
            }
        }
        //kiem tra HS Chuyen can
        if (CheckCC == 1) {
            cHSCC = "Hệ số chuyên cần phải là số!\n";
        } else {
            if (hesoCC <= 0) {
                cHSCC = "Hệ số chuyên cần phải lớn hơn 0!\n";
            }
        }

        //he so giua ki
        if (CheckGK == 1) {
            cHSGK = "Hệ số giữa kì phải là số!\n";
        } else {
            if (hesoGK <= 0) {
                cHSGK = "Hệ số giữa kì phải lớn hơn 0!\n";
            }
        }
        //he so ket thuc
        if (CheckKT == 1) {
            cHSKT = "Hệ số kết thúc phải là số!\n";
        } else {
            if (hesoKT <= 0) {
                cHSKT = "Hệ số kết thúc phải lớn hơn 0!\n";
            }
        }
        return cMamon + cTenmon + cSoTC + cHSGK + cHSKT;
    }

    public void Them() throws Exception {
//        LoadInput();        
        if (CheckInput().equals("")) {
            Statement st = conn.getCon().createStatement();
            int rs = st.executeUpdate("insert into monHoc values('" + maMon + "',N'" + tenMon + "'," + SoTC + "," + hesoCC + "," + hesoGK + "," + hesoKT + ");");
            JOptionPane.showMessageDialog(rootPane, "Thêm thành công");
            clearTF();
        } else {
            JOptionPane.showMessageDialog(rootPane, CheckInput());
        }

    }

    public void checkTextField(JTextField jtf, String strName) {
        if (jtf.getText().trim().equals("")) {
            JOptionPane.showMessageDialog(null, strName + " không được để trống");
            jtf.requestFocus();
        }
    }

    public void checkFormatedField(JFormattedTextField jtf, String strName) {
        if (jtf.getText().trim().equals("")) {
            JOptionPane.showMessageDialog(null, strName + " không được để trống và bắt buộc nhập số!");
            jtf.requestFocus();
        }
    }

    public void Sua() throws Exception {
        checkTextField(tfMaMH, "Mã môn học");
        checkTextField(tfTenMH, "Tên môn học");
        checkFormatedField(fmSoTC, "Số tín chỉ");
        checkFormatedField(fmHSCC, "Hệ số chuyên cần");
        checkFormatedField(fmHSGK, "Hệ số giữa kỳ");
        checkFormatedField(fmHSKT, "Hệ số kết thúc");

        maMon = tfMaMH.getText();
        tenMon = tfTenMH.getText();
        SoTC = Float.parseFloat(fmSoTC.getText());
        hesoCC = Float.parseFloat(fmHSCC.getText());
        hesoGK = Float.parseFloat(fmHSGK.getText());
        hesoKT = Float.parseFloat(fmHSKT.getText());

        PreparedStatement ps = ConSQL.getCon().prepareStatement("update monHoc set tenMH=N'" + tenMon + "',SoTC=" + SoTC + ",HSChuyenCan=" + hesoCC + ",HSGiuaky=" + hesoGK + ",HSThiKetthuc=" + hesoKT + " where maMH='" + maMon + "'");
        int kq = ps.executeUpdate();
        if (kq > 0) {
            JOptionPane.showMessageDialog(null, "Cập nhật thành công");
            LoadInformation();
        } else {
            JOptionPane.showMessageDialog(null, "Cập nhật thất bại! T-T");
        }
    }

    public void Xoa() throws Exception {
        maMon = tfMaMH.getText().trim();
        if (maMon.trim().equals("")) {
            JOptionPane.showMessageDialog(null, "Mã không được để trống!");
        } else {
            PreparedStatement ps = ConSQL.getCon().prepareStatement("delete from monHoc where maMH = '" + maMon + "'");
            int kq = ps.executeUpdate();
            if (kq > 0) {
                JOptionPane.showMessageDialog(null, "Xóa thành công");
                LoadInformation();
                clearTF();
            } else {
                JOptionPane.showMessageDialog(null, "Xóa không thành công");
            }
        }
    }

    public void Tim() throws Exception {

        int iChon = cbbSearch.getSelectedIndex();
        String str = tfSearch.getText();
//       loadSearch(str);
        if (iChon == 0) {
            LoadSearch(str, 0);
        } else {
            LoadSearch(str, 1);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        lbMaPB = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        lbTenPB = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        tfMaMH = new javax.swing.JTextField();
        tfTenMH = new javax.swing.JTextField();
        fmSoTC = new javax.swing.JFormattedTextField();
        fmHSCC = new javax.swing.JFormattedTextField();
        fmHSGK = new javax.swing.JFormattedTextField();
        fmHSKT = new javax.swing.JFormattedTextField();
        jPanel3 = new javax.swing.JPanel();
        btnThem = new javax.swing.JButton();
        btnSua = new javax.swing.JButton();
        btnXoa = new javax.swing.JButton();
        btnNhapLai = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbContent = new javax.swing.JTable();
        tfSearch = new javax.swing.JTextField();
        cbbSearch = new javax.swing.JComboBox();
        btnSearch = new javax.swing.JButton();
        btnXemTatCaDanhSach = new javax.swing.JButton();
        btnThoat = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(255, 255, 255));

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Thông tin phòng ban"));
        jPanel1.setFont(new java.awt.Font("Tahoma", 0, 14));

        jLabel1.setText("Mã phòng ban :");

        lbMaPB.setText("Mã");

        jLabel3.setText("Tên phòng ban :");

        lbTenPB.setText("Tên");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(lbTenPB, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lbMaPB, javax.swing.GroupLayout.DEFAULT_SIZE, 92, Short.MAX_VALUE))
                .addContainerGap(56, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(lbMaPB))
                .addGap(26, 26, 26)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(lbTenPB))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Thông tin môn học"));
        jPanel2.setFont(new java.awt.Font("Tahoma", 0, 14));

        jLabel5.setText("Mã môn học :");

        jLabel2.setText("Tên môn học :");

        jLabel4.setText("Số tín chỉ :");

        jLabel6.setText("Hệ số chuyên cần :");

        jLabel7.setText("Hệ số giữa kỳ :");

        jLabel8.setText("Hệ số thi kết thúc :");

        fmSoTC.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                fmSoTCKeyTyped(evt);
            }
        });

        fmHSCC.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                fmHSCCKeyTyped(evt);
            }
        });

        fmHSGK.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                fmHSGKKeyTyped(evt);
            }
        });

        fmHSKT.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                fmHSKTKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(fmSoTC)
                    .addComponent(tfMaMH)
                    .addComponent(tfTenMH, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel8)
                        .addGap(18, 18, 18)
                        .addComponent(fmHSKT))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel7))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(fmHSGK)
                            .addComponent(fmHSCC, javax.swing.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE))))
                .addContainerGap(128, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6)
                    .addComponent(tfMaMH, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(fmHSCC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(29, 29, 29)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel7)
                    .addComponent(tfTenMH, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(fmHSGK, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel8)
                    .addComponent(fmSoTC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(fmHSKT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setBackground(new java.awt.Color(255, 255, 255));

        btnThem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Button-Add-icon (1).png"))); // NOI18N
        btnThem.setText("Thêm");
        btnThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThemActionPerformed(evt);
            }
        });

        btnSua.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Edit-Document-icon.png"))); // NOI18N
        btnSua.setText("Sửa");
        btnSua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSuaActionPerformed(evt);
            }
        });

        btnXoa.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Button-Delete-icon (1).png"))); // NOI18N
        btnXoa.setText("Xóa");
        btnXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnXoaActionPerformed(evt);
            }
        });

        btnNhapLai.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Button-Refresh-icon.png"))); // NOI18N
        btnNhapLai.setText("Nhập lại");
        btnNhapLai.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNhapLaiActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnThem, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(91, 91, 91)
                .addComponent(btnSua, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 83, Short.MAX_VALUE)
                .addComponent(btnXoa, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(68, 68, 68)
                .addComponent(btnNhapLai)
                .addGap(52, 52, 52))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnThem)
                    .addComponent(btnNhapLai, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSua)
                    .addComponent(btnXoa))
                .addContainerGap(21, Short.MAX_VALUE))
        );

        jPanel4.setBackground(new java.awt.Color(255, 255, 255));
        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Danh sách"));

        tbContent.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "Mã môn học", "Tên môn học", "Số tín chỉ", "Hệ số chuyên cần", "Hệ số giữa kỳ", "Hệ số kết thúc"
            }
        ));
        tbContent.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbContentMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbContent);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 641, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        tfSearch.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tfSearchKeyPressed(evt);
            }
        });

        cbbSearch.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        btnSearch.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Search-icon.png"))); // NOI18N
        btnSearch.setText("Tìm kiếm");
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });

        btnXemTatCaDanhSach.setText("Xem tất cả danh sách");
        btnXemTatCaDanhSach.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnXemTatCaDanhSachActionPerformed(evt);
            }
        });

        btnThoat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Turn off.png"))); // NOI18N
        btnThoat.setText("Thoát");
        btnThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThoatActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(tfSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(cbbSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(29, 29, 29)
                                .addComponent(btnSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(btnXemTatCaDanhSach)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnThoat, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbbSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnXemTatCaDanhSach, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnThoat))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void btnThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThemActionPerformed
// TODO add your handling code here:
    try {
        Them();
    } catch (Exception ex) {
        Logger.getLogger(frmQuanLyMonHoc.class.getName()).log(Level.SEVERE, null, ex);
    }

}//GEN-LAST:event_btnThemActionPerformed

private void btnSuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSuaActionPerformed
// TODO add your handling code here:
    try {
        // TODO add your handling code here:
        Sua();
        btnSua.setEnabled(false);
        btnXoa.setEnabled(false);
    } catch (Exception ex) {
        Logger.getLogger(frmQuanLyMonHoc.class.getName()).log(Level.SEVERE, null, ex);
    }

}//GEN-LAST:event_btnSuaActionPerformed

private void btnXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnXoaActionPerformed
    int res = JOptionPane.showConfirmDialog(null, "Bạn có muốn xóa không?", "Confirm", JOptionPane.YES_NO_OPTION);
    if (res == 0) {
        try {
            Xoa();
            btnSua.setEnabled(false);
            btnXoa.setEnabled(false);
        } catch (Exception ex) {
            Logger.getLogger(frmQuanLyMonHoc.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}//GEN-LAST:event_btnXoaActionPerformed

private void btnNhapLaiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNhapLaiActionPerformed
// TODO add your handling code here:
    clearTF();
}//GEN-LAST:event_btnNhapLaiActionPerformed

private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
// TODO add your handling code here:
    try {
        // TODO add your handling code here:
        Tim();
    } catch (Exception ex) {
        Logger.getLogger(frmQuanLyMonHoc.class.getName()).log(Level.SEVERE, null, ex);
    }
}//GEN-LAST:event_btnSearchActionPerformed

private void btnXemTatCaDanhSachActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnXemTatCaDanhSachActionPerformed
    LoadInformation();
}//GEN-LAST:event_btnXemTatCaDanhSachActionPerformed

private void btnThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThoatActionPerformed
// TODO add your handling code here:
    new frmPhongBan(maPB, tenPB, matkhau).setVisible(true);
    this.setVisible(false);
}//GEN-LAST:event_btnThoatActionPerformed

private void tbContentMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbContentMouseClicked
// TODO add your handling code here:
//    LoadInformation();
    int iDongChon = tbContent.getSelectedRow();
    tfMaMH.setText((String) tbContent.getModel().getValueAt(iDongChon, 0));
    tfTenMH.setText((String) tbContent.getModel().getValueAt(iDongChon, 1));
    fmSoTC.setText("" + (int) (tbContent.getModel().getValueAt(iDongChon, 2)));
    fmHSCC.setText("" + (float) (tbContent.getModel().getValueAt(iDongChon, 3)));
    fmHSGK.setText("" + (float) tbContent.getModel().getValueAt(iDongChon, 4));
    fmHSKT.setText("" + (float) tbContent.getModel().getValueAt(iDongChon, 5));

    btnSua.setEnabled(true);
    btnXoa.setEnabled(true);
}//GEN-LAST:event_tbContentMouseClicked

private void tfSearchKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfSearchKeyPressed
// TODO add your handling code here:
    if (evt.getKeyCode() == 10) {
        btnSearch.doClick();
    }
}//GEN-LAST:event_tfSearchKeyPressed

private void fmSoTCKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_fmSoTCKeyTyped
// TODO add your handling code here:
    if (!Character.isDigit(evt.getKeyChar())){
        evt.consume();
    }
}//GEN-LAST:event_fmSoTCKeyTyped

private void fmHSCCKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_fmHSCCKeyTyped
// TODO add your handling code here:
     if (!Character.isDigit(evt.getKeyChar())){
        evt.consume();
    }
}//GEN-LAST:event_fmHSCCKeyTyped

private void fmHSGKKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_fmHSGKKeyTyped
// TODO add your handling code here:
     if (!Character.isDigit(evt.getKeyChar())){
        evt.consume();
    }
}//GEN-LAST:event_fmHSGKKeyTyped

private void fmHSKTKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_fmHSKTKeyTyped
// TODO add your handling code here:
     if (!Character.isDigit(evt.getKeyChar())){
        evt.consume();
    }
}//GEN-LAST:event_fmHSKTKeyTyped

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(frmQuanLyMonHoc.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(frmQuanLyMonHoc.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(frmQuanLyMonHoc.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(frmQuanLyMonHoc.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new frmQuanLyMonHoc().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnNhapLai;
    private javax.swing.JButton btnSearch;
    private javax.swing.JButton btnSua;
    private javax.swing.JButton btnThem;
    private javax.swing.JButton btnThoat;
    private javax.swing.JButton btnXemTatCaDanhSach;
    private javax.swing.JButton btnXoa;
    private javax.swing.JComboBox cbbSearch;
    private javax.swing.JFormattedTextField fmHSCC;
    private javax.swing.JFormattedTextField fmHSGK;
    private javax.swing.JFormattedTextField fmHSKT;
    private javax.swing.JFormattedTextField fmSoTC;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbMaPB;
    private javax.swing.JLabel lbTenPB;
    private javax.swing.JTable tbContent;
    private javax.swing.JTextField tfMaMH;
    private javax.swing.JTextField tfSearch;
    private javax.swing.JTextField tfTenMH;
    // End of variables declaration//GEN-END:variables
}
